<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  
  <title>event | H5教案</title>

  
  <meta name="author" content="John Doe">
  

  
  <meta name="description" content="5、事件模块是events
1const EventEmitter=require(&amp;quot;events&amp;quot;);
在Nodejs中事件的侦听者和事件对象是同一个，也就是说事件侦听对象自身就是一个事件直到node 14以后才新增了事件对象1234567const EventEmitter=">
  

  
  
  <meta name="keywords" content="">
  

  <meta id="viewport" name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, minimal-ui">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">

  <meta property="og:title" content="event"/>

  <meta property="og:site_name" content="H5教案"/>

  
  <meta property="og:image" content="/ericxie-book/favicon.ico"/>
  

  <link href="/ericxie-book/favicon.ico" rel="icon">
  <link rel="alternate" href="/ericxie-book/atom.xml" title="H5教案" type="application/atom+xml">
  <link rel="stylesheet" href="/ericxie-book/css/style.css" media="screen" type="text/css">
<meta name="generator" content="Hexo 5.3.0"></head>


<body>
<div class="blog">
  <div class="content">

    <header>
  <div class="site-branding">
    <h1 class="site-title">
      <a href="/ericxie-book/">H5教案</a>
    </h1>
    <p class="site-description"></p>
  </div>
  <nav class="site-navigation">
    <ul>
      
    </ul>
  </nav>
</header>

    <main class="site-main posts-loop">
    <article>

  
    
    <h3 class="article-title"><span>event</span></h3>
    
  

  <div class="article-top-meta">
    <span class="posted-on">
      <a href="/ericxie-book/2021/01/26/event/" rel="bookmark">
        <time class="entry-date published" datetime="2021-01-26T03:34:18.000Z">
          2021-01-26
        </time>
      </a>
    </span>
  </div>


  

  <div class="article-content">
    <div class="entry">
      
        <h2 id="5、事件"><a href="#5、事件" class="headerlink" title="5、事件"></a>5、事件</h2><p>模块是events</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">const EventEmitter=require(<span class="string">&quot;events&quot;</span>);</span><br></pre></td></tr></table></figure>
<h3 id="在Nodejs中事件的侦听者和事件对象是同一个，也就是说事件侦听对象自身就是一个事件"><a href="#在Nodejs中事件的侦听者和事件对象是同一个，也就是说事件侦听对象自身就是一个事件" class="headerlink" title="在Nodejs中事件的侦听者和事件对象是同一个，也就是说事件侦听对象自身就是一个事件"></a>在Nodejs中事件的侦听者和事件对象是同一个，也就是说事件侦听对象自身就是一个事件</h3><h4 id="直到node-14以后才新增了事件对象"><a href="#直到node-14以后才新增了事件对象" class="headerlink" title="直到node 14以后才新增了事件对象"></a>直到node 14以后才新增了事件对象</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">const EventEmitter=require(<span class="string">&quot;events&quot;</span>);</span><br><span class="line">var event=new EventEmitter();</span><br><span class="line">event.on(<span class="string">&quot;play&quot;</span>,<span class="keyword">function</span>(data)&#123;</span><br><span class="line">    console.log(data);</span><br><span class="line">&#125;);</span><br><span class="line">event.emit(<span class="string">&quot;play&quot;</span>);</span><br><span class="line">event.emit(<span class="string">&quot;play&quot;</span>,<span class="string">&quot;nihao&quot;</span>)</span><br></pre></td></tr></table></figure>
<h4 id="事件也可以继承"><a href="#事件也可以继承" class="headerlink" title="事件也可以继承"></a>事件也可以继承</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">const EventEmitter=require(<span class="string">&quot;events&quot;</span>);</span><br><span class="line">class MyEventEmitter extends EventEmitter&#123;&#125;</span><br><span class="line">var event=new MyEventEmitter();</span><br><span class="line"><span class="comment"># 仅处理一次事件侦听</span></span><br><span class="line">event.once(<span class="string">&quot;play&quot;</span>,<span class="keyword">function</span>(data)&#123;</span><br><span class="line">    console.log(data);</span><br><span class="line">&#125;);</span><br><span class="line">event.emit(<span class="string">&quot;play&quot;</span>);</span><br><span class="line">event.emit(<span class="string">&quot;play&quot;</span>,<span class="string">&quot;nihao&quot;</span>)</span><br></pre></td></tr></table></figure>
<h4 id="删除事件"><a href="#删除事件" class="headerlink" title="删除事件"></a>删除事件</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">const EventEmitter=require(<span class="string">&quot;events&quot;</span>);</span><br><span class="line">var event=new EventEmitter();</span><br><span class="line">event.on(<span class="string">&quot;play&quot;</span>,eventHandler);</span><br><span class="line">event.emit(<span class="string">&quot;play&quot;</span>);</span><br><span class="line">event.emit(<span class="string">&quot;play&quot;</span>,<span class="string">&quot;nihao&quot;</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> eventHandler(data)&#123;</span><br><span class="line">    event.off(<span class="string">&quot;play&quot;</span>,eventHandler);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
      
    </div>

  </div>

  <div class="article-footer">
    <div class="article-meta pull-left">

    

    

    </div>

    
  </div>
</article>

  






    </main>

    <footer class="site-footer">
  <p class="site-info">
    Proudly powered by <a href="https://hexo.io/" target="_blank">Hexo</a> and
    Theme by <a href="https://github.com/CodeDaraW/Hacker" target="_blank">Hacker</a>
    </br>
    
    &copy; 2021 John Doe
    
  </p>
</footer>
    
    
  </div>
</div>
</body>
</html>